var vue = new Vue({
	el : '#housePage',
	data : {
		positionCurrentPage : 1,
		positionPageSize : 5,
		positionTotalSize : 100,
		
		houseCurrentPage : 1,
		housePageSize : 5,
		housePositionTotalSize : 100,
		
		houseTable : [],
		positionTable : [],
		
		addHouseBtn : false,
		
		addPositionBtn : false,
		
		disableQuery : false,
		
		houseAddForm : {
			houseName : '',
			housePosition : '',
			houseDesc : '',
			houseClass : ''
		},
		
		positionAddForm : {
			houseName : '',
			houseId : '',
			positionName : '',
			positionPoi : '',
			positionDesc : '',
			positionClass : ''
		},
		
		allHouse : [],
		
		houseSelectForm : {
			houseName : '',
			housePosition : '',
			houseDesc : '',
			houseClass : '',
			id : ''
		},
		
		//form校验规则
		 houseRules:{
			 houseName:[
	            { required: true, message: '请输入仓库名称', trigger: 'blur' },
		        {min : 2, max : 8,message:'需要在2~8个字符或文字之间'}
	          ],
	          housePosition:[
		        { required: true, message: '请输入仓库地址', trigger: 'blur' },
		        {min : 2, max : 8,message:'需要在2~8个字符或文字之间'}
		      ],
		      houseDesc:[
			    { required: true, message: '请输入仓库描述', trigger: 'blur' },
		        {min : 2, max : 8,message:'需要在2~8个字符或文字之间'}
			  ]
		  },
		  positionRules : {
			  houseName:[
		        { required: true, message: '仓库必须选择', trigger: 'blur' },
		        {min : 2, max : 8,message:'需要在2~8个字符或文字之间'}
		      ],
			  positionName:[
		         { required: true, message: '请输入仓库库位名称', trigger: 'blur' },
			        {min : 2, max : 8,message:'需要在2~8个字符或文字之间'}
		      ],
		      positionPoi:[
			     { required: true, message: '请输入仓库库位地址', trigger: 'blur' },
			        {min : 2, max : 8,message:'需要在2~8个字符或文字之间'}
			   ],
			   positionDesc:[
				 { required: true, message: '请输入仓库库位描述', trigger: 'blur' },
			        {min : 2, max : 8,message:'需要在2~8个字符或文字之间'}
			  ]
		  }
		
	},
	mounted(){
		this.onQuery(1,5);
	},
	methods : {
		selectHouseValue(val){
			if(val == null || val == ''){
				return;
			}
			this.disableQuery = true;
			var houseId = this.houseSelectForm.id;
			this.queryPosition(houseId,this.positionCurrentPage,this.positionPageSize);
			
			let obj = {};
			obj = this.allHouse.find((item)=>{
				return item.id === val;
			});
			
			this.houseSelectForm.houseName = obj.houseName;
			this.positionAddForm.houseName = obj.houseName;
			this.positionAddForm.houseId = houseId;
		},
		addPosition(id){
			this.addPositionBtn = true;
			this.$refs[id].validate((valid)=>{
				if(valid){
					$.ajax({
						
						url : COMMON_URL.URL+'housAndPositionController/savePosition',
						type : 'post',
						contentType : 'application/json',
						data : JSON.stringify(vue.positionAddForm),
						success : function(data){
							vue.addPositionBtn = false;
							if(data.ok){
								vue.closePopper(id);
								vue.queryPosition(vue.positionAddForm.houseId,
												  vue.positionCurrentPage,
												  vue.positionPageSize);
							}else{
								swal(data.msg,'','');
							}
						},
						error : function(data){
							vue.addPositionBtn = false;
							swal('查询失败','','error');
						}
						
					});
				}else{
					this.addPositionBtn = false;
				}
			});
		},
		addHouse(id){
			this.addHouseBtn = true;
			this.$refs[id].validate((valid)=>{
				
				if(valid){
					
					$.ajax({
						
						url : COMMON_URL.URL + 'housAndPositionController/saveHouse',
						type : 'post',
						contentType : 'application/json',
						data : JSON.stringify(vue.houseAddForm),
						success : function(data){
							this.addHouseBtn = false;
							if(data.ok){
								vue.closePopper(id);
								vue.queryHouse(vue.houseCurrentPage,vue.housePageSize);
							}else{
								swal(data.msg,'','warning');
							}
						},
						error : function(data){
							this.addHouseBtn = false;
							swal('保存失败','','error');
						}
						
					});
					
				}else{
					this.addHouseBtn = false;
				}
				
			});
		},
		queryHouse(pageNum,pageSize){
			
			$.ajax({
				
				url : COMMON_URL.URL + 'housAndPositionController/findHouseForPage',
				type : 'get',
				dateType : 'json',
				data : {
					'pageNum' : pageNum,
					'pageSize' : pageSize
				},
				success : function(data){
					if(data.ok){
						var result = data.attrs;
						console.log(result);
						vue.housePositionTotalSize = result.totalElement;
						vue.houseTable = result.content;
					}else{
						swal(data.msg,'','warning');
					}
				},
				error : function(data){
					swal('查询失败','','error');
				}
				
				
			});
		},
		queryPosition(houseId,pageNum,pageSize){
			
			$.ajax({
				
				url : COMMON_URL.URL + 'housAndPositionController/findPositionForPage',
				type : 'get',
				dateType : 'json',
				data : {
					'pageNum' : pageNum,
					'pageSize' : pageSize,
					'houseId' : houseId
				},
				success : function(data){
					vue.disableQuery = false;
					if(data.ok){
						var result = data.attrs;
						vue.positionTotalSize = result.totalElement;
						vue.positionTable = result.content;
					}else{
						swal(data.msg,'','warning');
					}
				},
				error : function(data){
					swal('查询失败','','error');
				}
				
				
			});
		},
		closePopper(id){
			$('#closeButtonForClose').trigger('click');
			this.$refs[id].resetFields();
		},
		dateFormat(row,column){
			var date = row[column.property];  
	          if (date == undefined) {  
	             return "";  
	          }  
	          
	          date = new Date(date);
	          return formatDate(date, "yyyy-MM-dd hh:mm");
		},
		editClick(row){
			
			this.wareHouseVO2 = row;
		},
		positionHandleCurrentChange(val){
			if(this.houseSelectForm.id == null || this.houseSelectForm.id == ''){
				return;
			}
			this.positionCurrentPage = val;
			vue.queryPosition(this.houseSelectForm.id,val,this.positionPageSize);
		},
		positionHandleSizeChange(val){
			if(this.houseSelectForm.id == null || this.houseSelectForm.id == ''){
				return;
			}
			this.positionPageSize = val;
			vue.queryPosition(this.houseSelectForm.id,this.positionCurrentPage,val);
		},
		houseHandleSizeChange(val){
			this.housePageSize = val;
			this.queryHouse(this.houseCurrentPage,val);
		},
		houseHandleCurrentChange(val){
			this.houseCurrentPage = val;
			this.queryHouse(val,this.housePageSize);
		},
		onQuery(pageNum,pageSize){
			
			$.ajax({
				url : COMMON_URL.URL+'housAndPositionController/getHouseAndPosition',
				dataType : 'json',
				type : 'get',
				data : {
					'pageNum': pageNum,
					'pageSize' : pageSize
				},
				success : function(data){
					if(data.ok){
						var val = data.attrs;
						
						vue.positionTotalSize = val.positionSize;
						vue.positionTable = val.position;
						
						vue.housePositionTotalSize = val.houseSize;
						vue.houseTable = val.house;
						vue.allHouse = val.allHouse;

					}else{
						swal(data.msg,'','warning');
					}
				},
				error : function(data){
					swal('查询失败','','error');
				}
			});
			
		}
		
	}
});
